//
//  JYTChangePasswardViewController.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 17/1/16.
//  Copyright © 2017年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTChangePasswardViewController.h"
#import "JYTLoginHandler.h"
#import <SVProgressHUD.h>
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS

@interface JYTChangePasswardViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *midView;
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) UITextField *textFieldTop;
@property (nonatomic, strong) UITextField *textFieldMid;
@property (nonatomic, strong) UITextField *textFieldBottom;
@end

@implementation JYTChangePasswardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSDictionary *dic = @{@"lftBtnBgImg":@"ic_goback",
                          @"lftBtnTitle":@"no",
                          @"pageTitle":@"修改密码",
                          @"rhtBtnBgImg":@"no",
                          @"rhtBtnTitle":@"no"};
    
    [self setNavgationBarWithDic:dic];

    [self addSubView];
}


- (void)addSubView
{
    
    UIView *topView = [[UIView alloc]init];
    topView.backgroundColor = kColor(240, 240, 240);
    self.topView = topView;
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).offset(74);
        make.height.equalTo(@(40));
        
    }];
    
    UIView *lineTop = [[UIView alloc]init];
    lineTop.backgroundColor = kColor(226, 226, 226);
    [self.topView addSubview:lineTop];
    [lineTop mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.topView.mas_left);
        make.right.equalTo(self.topView.mas_right);
        make.top.equalTo(self.topView.mas_top);
        make.height.equalTo(@(0.5));
        
    }];
    
    UIView *lineBotom = [[UIView alloc]init];
    lineBotom.backgroundColor = kColor(226, 226, 226);
    [self.topView addSubview:lineBotom];
    [lineBotom mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.topView.mas_left);
        make.right.equalTo(self.topView.mas_right);
        make.bottom.equalTo(self.topView.mas_bottom);
        make.height.equalTo(@(0.5));
        
    }];
    
    
    UITextField *textFieldTop = [[UITextField alloc]init];
    textFieldTop.borderStyle = UITextBorderStyleNone;
    textFieldTop.backgroundColor = [UIColor clearColor];
    textFieldTop.font = [UIFont fontWithName:@"Arial" size:16.0f];
    textFieldTop.textColor = kColor(96, 96, 96);
    textFieldTop.delegate = self;
    textFieldTop.secureTextEntry = YES;
    textFieldTop.placeholder = @"当前密码";
    //textField.clearsOnBeginEditing = YES;
    //    textField.textAlignment = UITextAlignmentLeft;
    //    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.textFieldTop = textFieldTop;
    [self.topView addSubview:textFieldTop];
    [textFieldTop mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.topView.mas_left).offset(20);
        make.width.equalTo(@(CURRENT_SCREEN_WIDTH * 0.8));
        make.centerY.equalTo(self.topView.mas_centerY);
        
        
    }];
    
    
    
    
    UIView *midView = [[UIView alloc]init];
    midView.backgroundColor = kColor(240, 240, 240);
    self.midView = midView;
    [self.view addSubview:midView];
    [midView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.topView.mas_bottom).offset(5);
        make.height.equalTo(@(40));
        
    }];
    
    UIView *lineTopNew = [[UIView alloc]init];
    lineTopNew.backgroundColor = kColor(226, 226, 226);
    [self.midView addSubview:lineTopNew];
    [lineTopNew mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.midView.mas_left);
        make.right.equalTo(self.midView.mas_right);
        make.top.equalTo(self.midView.mas_top);
        make.height.equalTo(@(0.5));
        
    }];
    
    UIView *lineBotomNew = [[UIView alloc]init];
    lineBotomNew.backgroundColor = kColor(226, 226, 226);
    [self.midView addSubview:lineBotomNew];
    [lineBotomNew mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.midView.mas_left);
        make.right.equalTo(self.midView.mas_right);
        make.bottom.equalTo(self.midView.mas_bottom);
        make.height.equalTo(@(0.5));
        
    }];
    
    
    UITextField *textFieldMid = [[UITextField alloc]init];
    textFieldMid.borderStyle = UITextBorderStyleNone;
    textFieldMid.backgroundColor = [UIColor clearColor];
    textFieldMid.font = [UIFont fontWithName:@"Arial" size:16.0f];
    textFieldMid.textColor = kColor(96, 96, 96);
    textFieldMid.delegate = self;
    textFieldMid.secureTextEntry = YES;
    textFieldMid.placeholder = @"新密码";
    //textField.clearsOnBeginEditing = YES;
    //    textField.textAlignment = UITextAlignmentLeft;
    //    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.textFieldMid = textFieldMid;
    [self.midView addSubview:textFieldMid];
    [textFieldMid mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.midView.mas_left).offset(20);
        make.width.equalTo(@(CURRENT_SCREEN_WIDTH * 0.8));
        make.centerY.equalTo(self.midView.mas_centerY);
        
        
    }];
    
    
    UIView *bottomView = [[UIView alloc]init];
    self.bottomView = bottomView;
    bottomView.backgroundColor = kColor(240, 240, 240);
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.midView.mas_bottom).offset(5);
        make.height.equalTo(@(40));
        
    }];
    
    UIView *lineTopNewTwo = [[UIView alloc]init];
    lineTopNewTwo.backgroundColor = kColor(226, 226, 226);
    [self.bottomView addSubview:lineTopNewTwo];
    [lineTopNewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.bottomView.mas_left);
        make.right.equalTo(self.bottomView.mas_right);
        make.top.equalTo(self.bottomView.mas_top);
        make.height.equalTo(@(0.5));
        
    }];
    
    UIView *lineBotomNewTwo = [[UIView alloc]init];
    lineBotomNewTwo.backgroundColor = kColor(226, 226, 226);
    [self.bottomView addSubview:lineBotomNewTwo];
    [lineBotomNewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self.bottomView.mas_left);
        make.right.equalTo(self.bottomView.mas_right);
        make.bottom.equalTo(self.bottomView.mas_bottom);
        make.height.equalTo(@(0.5));
        
    }];
    
    
    UITextField *textFieldBottom = [[UITextField alloc]init];
    textFieldBottom.borderStyle = UITextBorderStyleNone;
    textFieldBottom.backgroundColor = [UIColor clearColor];
    textFieldBottom.font = [UIFont fontWithName:@"Arial" size:16.0f];
    textFieldBottom.textColor = kColor(96, 96, 96);
    textFieldBottom.delegate = self;
    textFieldBottom.secureTextEntry = YES;
    textFieldBottom.placeholder = @"确认新密码";
    //textField.clearsOnBeginEditing = YES;
    //    textField.textAlignment = UITextAlignmentLeft;
    //    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.textFieldBottom = textFieldBottom;
    [self.bottomView addSubview:textFieldBottom];
    [textFieldBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.bottomView.mas_left).offset(20);
        make.width.equalTo(@(CURRENT_SCREEN_WIDTH * 0.8));
        make.centerY.equalTo(self.bottomView.mas_centerY);
        
        
    }];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *normal =[self createImageWithColor:[UIColor colorWithRed:131/255.0f green:182/255.0f blue:248/255.0f alpha:1]];
    UIImage *highlighted =[self createImageWithColor:[UIColor colorWithRed:131/255.0f green:182/255.0f blue:248/255.0f alpha:1]];
    [btn setBackgroundImage:normal forState: UIControlStateNormal];
    [btn setBackgroundImage:highlighted forState: UIControlStateHighlighted];
    [btn setTitle:@"确认保存" forState:UIControlStateNormal];
    [btn setTitleColor:kColor(240, 240, 240) forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [btn addTarget:self action:@selector(commitIdear) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
        make.height.equalTo(@(49));
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIImage *)createImageWithColor:(UIColor *)color

{
    
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return theImage;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    UIView *vw = textField.superview;
    
    for (int i = 0; i < 2; i++) {
        
        UIView *subView = vw.subviews[i];
        subView.backgroundColor = kColor(112, 176, 223);
        
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    UIView *vw = textField.superview;
    
    for (int i = 0; i < 2; i++) {
        
        UIView *subView = vw.subviews[i];
        subView.backgroundColor = kColor(226, 226, 226);
        
    }
    
}

- (void)commitIdear
{
    NSString *old = self.textFieldTop.text;
    if (!old || [old isEqualToString:@""]) {
        [self showMessage:@"当前密码不能为空"];
        return;
    }
    //[NSUserDefaults standardUserDefaults]setObject:self.passwordTextField.text forKey:@"password"
    NSString *source = [[NSUserDefaults standardUserDefaults] valueForKey:@"password"];
    if (![old isEqualToString:source]) {
        [self showMessage:@"检查当前密码是否输入正确"];
        return;
    }
    
    NSString *pwdOne = self.textFieldMid.text;
    if (!pwdOne || [pwdOne isEqualToString:@""]) {
        [self showMessage:@"新密码不能为空"];
        return;
    }
    
    NSString *pwdTwo = self.textFieldBottom.text;
    if (!pwdTwo || [pwdTwo isEqualToString:@""]) {
         [self showMessage:@"确认新密码不能为空"];
        return;
    }
 
    
    if (![pwdOne isEqualToString:pwdTwo]) {
         [self showMessage:@"检查新密码是否输入一致"];
        return;
        
    }
    User * u = [[JYTUserManager shared] getUser];
    NSString *uName = u.userName;
    
     [SVProgressHUD showWithStatus:@"正在加载......"];
    [[JYTLoginHandler shared] changePwd:old andPwd:pwdOne andUserName:uName completionHandler:^(BOOL succ) {
        
        if (succ) {
            [SVProgressHUD dismiss];
            [self showMessage:@"修改密码成功"];
            [self goBack];
        } else {
            [SVProgressHUD dismiss];
            [self showMessage:@"修改密码失败"];
        }
    }];

}

- (void)showMessage:(NSString *)message
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    label.text = message;
    
    //    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290, 9000)];
    //初始化段落，设置段落风格
    
    NSMutableParagraphStyle *paragraphstyle=[[NSMutableParagraphStyle alloc]init];
    paragraphstyle.lineBreakMode=NSLineBreakByCharWrapping;
    
    //设置label的字体和段落风格
    
    NSDictionary *dic=@{NSFontAttributeName:label.font,NSParagraphStyleAttributeName:paragraphstyle.copy};
    
    //计算label的真正大小,其中宽度和高度是由段落字数的多少来确定的，返回实际label的大小
    
    CGRect rect=[label.text boundingRectWithSize:CGSizeMake(290, 9000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    CGSize LabelSize = CGSizeMake(rect.size.width, rect.size.height);
    label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:15];
    [showview addSubview:label];
    showview.frame = CGRectMake((CURRENT_SCREEN_WIDTH - LabelSize.width - 20)/2, CURRENT_SCREEN_HEIGHT - 100, LabelSize.width+20, LabelSize.height+10);
    [UIView animateWithDuration:2 animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
